<?


class CTRLmodule extends CONTROL {

#	In parent class:
#	function asset()
#	function ajax()

	function __construct( $vars ) {
		global $Page;
		
		$this->_addvars( $vars );
		
		// Create Page
		if(( $pid = $this->vars->parentid ) || $pid = $this->vars->page->id )
			$this->page = $Page = new PAGE( $pid );
		
		// Create Module
		$this->modcon = new MOD_CONSTRUCT();
		$this->mod = ( $this->action == "new" )?
			$this->modcon->create(( $this->vars->modtype )? $this->vars->modtype : "text.basic", $this->vars->ctype ) : $this->modcon->request_json( $this->vars->id );

		$this->mod->nameid = ( $this->mod->nameid )?  $this->mod->nameid : 0;
		$this->mod->textid = ( $this->mod->textid )?  $this->mod->textid : 0;
		
	}

#	Object list.
	function filter_list() {
		global $Request;
		
		return "
		ref.seed([
			new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
			lst = new List({ type: ref.vars.type, descr:'" . (( $this->vars->boxdescr )? $this->vars->boxdescr : $this->vars->descr ) . "', blank:'add {$this->vars->type}'})
		]);

		lst.seed( ". $Request->parse_list( "ID, ID as key_0, substring( json, 9, locate('\"', json, 9) - 9) as key_1, <%user:author%> as key_2" ) ." );";
		
	}
	
#	New Module.
	function filter_new() {
	
		$new = "$('$this->parent').seed([" . $this->modcon->render_core_json( $this->mod->id ) . "]);";

		return $new . $this->filter_edit();
	}
	
#	Edit Module.
	function filter_edit() {
		global $Engine;
		
		$opts = array( "'action: {$this->mod->action}'" );
		
		if( $this->mod->vars )
			foreach( $this->mod->vars as $key => $val ) if( is_string( $val ) || is_int( $val )) $opts[] = "new Ajxfield({ type:'module', descr:'$key', objid:{$this->mod->id}, field:'json.vars.$key', value:\"$val\" })";
		
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer module '". (( $this->mod->nameid )?  $Engine->text->get( $this->mod->nameid ) : $this->mod->id ) . "'\" ),
			new Textfield({ descr:'name module', type:'module', parentid:{$this->mod->id}, textid:{$this->mod->nameid}, values:". json_encode(  $Engine->text->filter( $this->mod->nameid )) ." }),
			new Htmlfield({ descr:'text module', type:'module', parentid:{$this->mod->id}, textid:{$this->mod->textid}, values:". json_encode(  $Engine->text->filter( $this->mod->textid, 'long' )) ." }),
			new Options({ descr:'Technische instellingen', floats: [" . implode( ",", $opts ) . "]})
		]);";

	}
	
#	Kill.
	function filter_kill() {
		
		$mods = $this->page->get( 'mods' );
			
		foreach( $mods->{ $this->vars->page->column } as $key => $val )
			if( $val != $this->vars->id ) $newcolumn[] = $val;
			
		$mods->{ $this->vars->page->column } = ( count( $newcolumn ))? $newcolumn : array();
				
		$this->page->set( "mods", $mods, true );
		
		return $this->comment( "Module removed." );
	}

#	Change hierarchy.
	function filter_sortable() {
		
		$mods = $this->page->get( "mods" );
		$mods->{ $this->vars->ctype } = array();
			
		if( count( $this->vars->floats ))
			foreach( $this->vars->floats as $md ) $mods->{ $this->vars->ctype }[] = $md->id;

		$this->page->set( "mods", $mods, true );
		
		return $this->comment( "Column hierarchy updated." );
	}

}





/*







#	Object PAGE.
if( $Request->vars->parentid || $Request->vars->page->id )
	$Page = new PAGE(( $Request->vars->parentid )? $Request->vars->parentid : $Request->vars->page->id );


#	Object MODULE.
$Modcon = new MOD_CONSTRUCT();
$Mod = ( $Request->action == "new" )?
			$Modcon->create(( $Request->vars->modtype )? $Request->vars->modtype : "text.basic", $Request->vars->ctype ) : $Modcon->request_json( $Request->vars->id );

$Mod->nameid = ( $Mod->nameid )?  $Mod->nameid : 0;
$Mod->textid = ( $Mod->textid )?  $Mod->textid : 0;

if( $Request->action == "edit" || $Request->action == "new" ) {

		if( $Request->action == "new" ) {
			?>
			
			$("<?=$Request->parent?>").seed([<?=$Modcon->render_core_json( $Mod->id )?>]);

			<?
		}
		
	?>
		ref.seed([
			
			new Element( "h1" ).update( "Beheer module '<?=( $Mod->nameid )?  $Engine->text->get( $Mod->nameid ) : $Mod->id ?>'" ),
			new Textfield({ descr:'name module', type:"module", parentid:<?=$Mod->id?>, textid:<?=$Mod->nameid?>, values:<?=json_encode(  $Engine->text->filter( $Mod->nameid ))?> }),
			new Htmlfield({ descr:'text module', type:"module", parentid:<?=$Mod->id?>, textid:<?=$Mod->textid?>, values:<?=json_encode(  $Engine->text->filter( $Mod->textid, "long" ))?> })
			
			//,
			//new Options({id:ref.id+".options", tool:'group.basic', descr:'Technische settings', floats: [
			//	
			//], meta:{}})
		]);
		
	<?
}


#	Ajax actions.

if( $Request->transfer_type == "push" ) {

#		Kill.
		if( $Request->action == "kill" ) {
			$mods = $Page->get( 'mods' );
			
			foreach( $mods->{ $Request->vars->page->column } as $key => $val )
				if( $val != $Request->vars->id ) $newcolumn[] = $val;
			
			$mods->{ $Request->vars->page->column } = ( count( $newcolumn ))? $newcolumn : array();
				
			$Page->set( "mods", $mods, true );
		
			?>
			dashboard.comment("Module removed.");
			<?
		}

#		Change hierarchy.
		else if( $Request->action == "sortable" ) {
			
			$mods = $Page->get( "mods" );
			$mods->{ $Request->vars->ctype } = array();
			
			if( count( $Request->vars->floats ))
				foreach( $Request->vars->floats as $md ) $mods->{ $Request->vars->ctype }[] = $md->id;

			$Page->set( "mods", $mods, true );
		
			?>
			dashboard.comment("Column hierarchy updated.");
			<?
		}
		
} */ ?>
